package com.zhengx.baselib.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.FrameLayout;

import com.github.ikidou.fragmentBackHandler.BackHandlerHelper;
import com.github.ikidou.fragmentBackHandler.FragmentBackHandler;
import com.zhengx.baselib.R;

/**
 * name：BaseDrawerFragment
 * class: 带侧滑菜单的BaseFragment
 * author: zhengx
 * create_time: 2019/1/23
 */

public abstract class BaseDrawerFragment extends Fragment implements FragmentBackHandler,
        IBaseView{

    protected View rootView;

    protected ViewStub toolbarViewStub;

    protected View mContentView;

    protected Context context;

    protected Activity mActivity;

    private boolean isStatusBarLightMode = false;

    private int statusBarColor = -2;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
        mActivity = (Activity) context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.layout_fragment_base_drawer, container, false);
        toolbarViewStub = (ViewStub) rootView.findViewById(R.id.toolbar_view_stub);
        setFragmentContentView(inflater, setContentView());

//        if (statusBarColor != -2) {
//
//        }
//        if (statusBarColor == -2) {
//            statusBarColor = getResources().getColor(R.color.colorPrimaryDark);
//        }
//        BarUtils.setStatusBarColor(getActivity(), statusBarColor, 0);
//        BarUtils.addMarginTopEqualStatusBarHeight(view);
//        BarUtils.setStatusBarLightMode(getActivity(), isStatusBarLightMode);
//        BarUtils.setNavBarImmersive(getActivity());
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            BarUtils.setNavBarColor(getActivity(), statusBarColor);
//        }
        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle bundle = getArguments();
        initArgData(bundle);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        bindView(savedInstanceState, mContentView);
        int toolbarLayoutId = setCustomToolbarLayout();
        toolbarViewStub.setLayoutResource(toolbarLayoutId == 0 ? R.layout.layout_toolbar_in_common :
                toolbarLayoutId);
        initToolbar(toolbarViewStub.inflate());
        doBusiness();
    }

    @Override
    public void onDestroyView() {
        if (mContentView != null) {
            ((ViewGroup) mContentView.getParent()).removeView(mContentView);
        }
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        context = null;
        mActivity = null;
    }

    /**
     * 设置共用的toolbar，子类中调用, 默认一个toolbar布局文件
     * @return
     */
    public int setCustomToolbarLayout() {
        return 0;
    }

    /**
     * 初始化共用的toolbar, 子类中调用
     * @param toolbarContent 头部具体文件
     */
    public void initToolbar(View toolbarContent) {

    }

    /**
     * 设置fragment布局内容（除去toolbar）
     * @param layoutInflater
     * @param layoutId
     */
    private void setFragmentContentView(LayoutInflater layoutInflater, @LayoutRes int layoutId) {
        if (layoutId == 0) return;
        FrameLayout contentContainer = rootView.findViewById(R.id.frag_base_container);
        mContentView = layoutInflater.inflate(layoutId, contentContainer, false);
        contentContainer.addView(mContentView);
    }

    public void setStatusBar(int statusBarColor, boolean isStatusBarLightMode) {
        this.statusBarColor = statusBarColor;
        this.isStatusBarLightMode = isStatusBarLightMode;
    }

    public void setStatusBar(boolean isStatusBarLightMode) {
        this.isStatusBarLightMode  = isStatusBarLightMode;
    }

    @Override
    public boolean onBackPressed() {
        return BackHandlerHelper.handleBackPress(this);
    }

    public <T extends View> T findViewById(@IdRes int id) {
        if (mContentView == null) throw new NullPointerException("ContentView is null.");
        return mContentView.findViewById(id);
    }
}
